﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace TYBase
{
    public static class ObjectExtend
    {
        public static string JsonAsString(this object vale)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(vale);
        }

        public static Dictionary<string, object> AsDict(this object obj)
        {
            var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var dictionary = new Dictionary<string, object>();

            foreach (var property in properties)
            {
                var value = property.GetValue(obj);
                dictionary.Add(property.Name, value);
            }

            return dictionary;
        }
    }
}
